package simpletests;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.UUID;

import voldemort.client.StoreClient;

public class PutABunchOfData implements Runnable {

	@SuppressWarnings("unchecked")
	private StoreClient client;
	int keyPrefix;
	String writeValue;
	int numberOfthreads;
	int numberOfKeysToGenerate;

	@SuppressWarnings("unchecked")
	public PutABunchOfData(int keyPrefix, String fileToPutToDataStore,
			int totalNumberOfKeysToGeneratePerThread, StoreClient client)
			throws IOException {
		this.keyPrefix = keyPrefix;
		this.numberOfKeysToGenerate = totalNumberOfKeysToGeneratePerThread;

		BufferedReader bufferedReader = new BufferedReader(new FileReader(
				fileToPutToDataStore));

		StringBuilder writeValue = new StringBuilder();

		String lineOfData;

		while ((lineOfData = bufferedReader.readLine()) != null)
			writeValue.append(lineOfData);

		this.writeValue = writeValue.toString();

		this.client = client;

	}

	@SuppressWarnings("unchecked")
	@Override
	public void run() {
		
		
		for (int keySuffix = 0; keySuffix < this.numberOfKeysToGenerate; keySuffix++) {
			StringBuilder key = new StringBuilder("Key");
			key.append(keyPrefix);
			key.append("-");
			key.append(keySuffix);
			try {
				client.put(UUID.randomUUID().toString(), writeValue);
			} catch (Exception e) {
				System.out.println("Exception: " + e.getMessage());
			}
		}
	}

	@SuppressWarnings("unchecked")
	public void delete() {
		for (int i = 0; i < this.numberOfKeysToGenerate; i++) {
			StringBuilder key = new StringBuilder("Key");
			key.append(keyPrefix);
			key.append("-");
			key.append(i);
			client.delete(key.toString());
		}
	}

}
